<script>
    $(function() {
        $("#desde").datepicker({dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true});
        $( "#hasta" ).datepicker({dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true});
    });
</script> 
<style>
    .input-mini{width: 20px;}
</style>

<div class="row-fluid">
    <?php echo $this->render('/listados/menu.phtml'); ?>


    <div class="well well-small">
        <form name="form_busqueda" class="form-inline noPrint" method="GET" action="<?php echo $this->baseUrl(); ?>/admin/listados/mailings">
            <legend>Mailings</legend>
            <?php $hoy = new DateTime(); ?>
            <label class="control-label">Desde</label>
            <input id="desde" name="desde" value="<?php echo (empty($this->desde)) ? $hoy->format("d/m/Y") : $this->desde; ?>" class="input-small" type="text" />
            <label class="control-label">Hasta</label>
            <input id="hasta" name="hasta"   value="<?php echo (empty($this->hasta)) ? $hoy->format("d/m/Y") : $this->hasta; ?>" class="input-small" type="text" />
            
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label>Sucursal</label>
            <select id="sucursal" name="sucursal">
                <option value="0">Todas</option>
                <?php foreach ($this->sucursales as $sucursal) { ?>
                <option <?php echo $this->sucursal == $sucursal->getId() ? "selected" : "" ?> value="<?php echo $sucursal->getId(); ?>">
                    <?php echo $sucursal->getDescripcion(); ?></option>
                <?php } ?>
            </select>
            
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label class="control-label">Con credito</label>
            <select name="cred_cancelados" class="input-medium">
                <option value="0" <?php echo $this->cred_cancelados == 0 ? "selected" : "" ?>>Seleccione ...</option>
                <option value="1" <?php echo $this->cred_cancelados == 1 ? "selected" : "" ?>>Que halla cancelado al menos una vez</option>
                <option value="2" <?php echo $this->cred_cancelados == 2 ? "selected" : "" ?>>Que nunca halla cancelado</option>
            </select>
            
            &nbsp;&nbsp;&nbsp;
            <label class="control-label">Al menos&nbsp;</label>
            <select name="cant_creditos_activos" class="input-small">
                <option value="" <?php echo empty($this->cant_creditos_activos) ? "selected" : "" ?>>Seleccione ...</option>
                <option value="0" <?php echo $this->cant_creditos_activos == "0" ? "selected" : "" ?>>0</option>
                <option value="1" <?php echo $this->cant_creditos_activos == "1" ? "selected" : "" ?>>1</option>
                <option value="2" <?php echo $this->cant_creditos_activos == "2" ? "selected" : "" ?>>2</option>
                <option value="3" <?php echo $this->cant_creditos_activos == "3" ? "selected" : "" ?>>3</option>
                <option value="4" <?php echo $this->cant_creditos_activos == "4" ? "selected" : "" ?>>4</option>
                <option value="5" <?php echo $this->cant_creditos_activos == "5" ? "selected" : "" ?>>5</option>
            </select>
            <label class="control-label">&nbsp;creditos activos</label>

            <br/><br/>
            
            <label class="control-label" >Al menos &nbsp;</label>
            <input id="cuotas_pagas" name="cuotas_pagas"   value="<?php echo (empty($this->cuotas_pagas)) ? 0 : $this->cuotas_pagas; ?>" class="input-mini" type="text" /><label class="control-label">&nbsp;cuotas pagas</label>
            
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label class="control-label">Menos de &nbsp;</label>
            <input id="mora" name="mora" value="<?php echo (empty($this->mora)) ? 0 : $this->mora; ?>" class="input-mini" type="text" /> <label class="control-label">d&iacute;as de mora</label>
            
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label class="control-label">M&aacute;s de &nbsp;</label><input id="cant_creditos" name="cant_creditos" value="<?php echo (empty($this->cant_creditos)) ? 0 : $this->cant_creditos; ?>" class="input-mini" type="text" /> creditos
            
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="checkbox" name="dos_cuotas" value="1" <?php echo $this->dos_cuotas == "1" ? "checked" : "" ?> >&nbsp;Con 2 Cuotas faltantes a pagar
            
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            <input class="btn" value="Buscar" type="submit" />
        </form>

        <form action="<?php echo $this->baseUrl() ?>/admin/listados/generarcartasmorosos/" target="_blank">
            <div class="row-fluid">
                <?php
                $subtitulo = '<b>' . count($this->personas) . '</b> Ventas encontrados entre <b>' . $this->desde . '</b> a <b>' . $this->hasta . '</b>';
                echo $subtitulo;
                ?>
                <div class="btn-toolbar pull-right">
                    <button id="exportar_excel" class="btn btn-success noPrint" type="button"><i class=" icon-download icon-white"></i>  Exportar a Excel</button>
                    <input type="submit" class="btn btn-danger" value="Generar Cartas">
                    <button type="button" onclick="PrintElement('#tabla_mailings')" class="btn btn-inverse noPrint"><i class=" icon-print icon-white"></i>  Imprimir</button>&nbsp;&nbsp;
                </div>
            </div>

            <div class="row-fluid ">

                <table id="tabla_mailings" class="tabla_reducida">
                    <thead>
                    <th>Apellido y Nombre</th><th>DNI</th><th style="width:80px;" class="noPrint"><input type="checkbox" id="clientes_check" onchange="MarcarCheckbox(this.id,'contenedor');"   value=""></th><th style="width:80px;" class="noPrint"><input type="checkbox" id="garantes_check" onchange="MarcarCheckbox(this.id,'contenedor');"   value=""></th>
                    </thead>
                    <tbody>
                        <?php if (count($this->personas)) { ?>
                            <?php $total = 0; ?>
                            <?php $i = 0; ?>
                            <?php $cliente = new Doctrine_Model_Entities_Persona(); ?>

                            <?php foreach ($this->personas as $cliente) { ?>
                                <tr>
                                    <td>
                                        <a href="<?php echo $this->baseUrl("usuarios/busqueda/obternerdatospersona/persona_id/{$cliente->getId()}") ?>">
                                            <?php echo $cliente->getApellido() . ", " . $cliente->getNombres(); ?>
                                        </a>
                                    </td>
                                    <td style="width:90px;text-align:center;"><?php echo $cliente->getNroDocumento(); ?></td>
                                    <td style="width:90px;" class="noPrint">
                                        <input type="checkbox" id="cliente_check_<?php echo $i; ?>" name="clientes[]" cred_id="<?php echo $cliente->getId(); ?>" value="<?php echo $cliente->getId(); ?>" class="cliente_checkbox">
                                    </td>
                                    <td style="width:90px;" class="noPrint">
                                        <?php
                                        $creditos_activos = array();
                                        $creditos_activos = $cliente->obtenerCreditosActivos();
                                        ?>
                                        <?php foreach ($creditos_activos as $cred_activo): ?>
                                            <?php if ($cred_activo->getGarante()): ?>
                                                <?php echo $cred_activo->getNumero() ?>
                                                <input title="<?php echo "{$cred_activo->getGarante()->getApellido()}, {$cred_activo->getGarante()->getNombres()}" ?>" type="checkbox" id="garante_check_<?php echo $i . "_" . $cred_activo->getGarante()->getId(); ?>" name="garantes[]" value="<?php echo $cred_activo->getGarante()->getId(); ?>" class="garante_checkbox">
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    </td>

                                </tr>
                                <?php $i++; ?>
                            <?php } ?>
                        <?php } else { ?>
                            <tr>
                                <td colspan="8">Sin Resultados</td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>

            </div>
        </form>
    </div>

</div>



<script type="text/javascript">
    
    $(document).ready(function() {
        $("#tabla_mailings").chromatable({
            width: "100%",  
            height: "350px",
            scrolling: "yes"
        });
        
        $("#exportar_excel").click(function(){
            var creditos = $(".cliente_checkbox:checked");
            var ids = new Array();
            $.each(creditos, function(i,val){
                ids.push($(val).attr("cred_id"));
            });
            
            var url = "<?php echo $this->baseUrl("/admin/listados/exportarmailingexcel"); ?>";
            url += "/ids/"+ids;
            $(location).attr("href", url);           
        });
        
    });
    
    function MarcarCheckbox(id, div) {
        // $("." + div + " :checkbox").attr('checked', $('#' + id).is(':checked'));
    
        $("input[type=checkbox]").each(function() { 
        
            if(id=='clientes_check'){
                if($(this).attr('class')=='cliente_checkbox'){
                    if($("#clientes_check").is(":checked")){
                        $(this).attr("checked","checked");
                    }else{
                        $(this).removeAttr("checked");
                       
                    }
                } 
            }else{
                if($(this).attr('class')=='garante_checkbox'){
                    if($("#garantes_check").is(":checked")){
                        $(this).attr("checked","checked");
                    }else{
                        $(this).removeAttr("checked");
                        
                    }
                }   
            }
       
         
        }); 
    }
    
    //Simple wrapper to pass a jQuery object to your new window
    function PrintElement(elem){
        Popup($(elem).html());
    }

    //Creates a new window and populates it with your content
    function Popup(data) {
        //Create your new window
        var w = window.open('', 'Mailings <?php echo $this->desde ?> - <?php echo $this->hasta ?>', 'height=600,width=800');
        w.document.write('<html><head><title>Mailings <?php echo $this->desde ?> - <?php echo $this->hasta ?></title>');
        //Include your stylesheet (optional)
        w.document.write('<link rel="stylesheet" href="<?php echo $this->baseUrl("/bootstrap/css/bootstrap.css") ?>" type="text/css" />');
        w.document.write('</head><body>');
        w.document.write('<h2>Mailings <?php echo $this->desde ?> - <?php echo $this->hasta ?></h2>');
        //Write your content
        w.document.write("<table class='table table-bordered'>"+data+"</table>");
        w.document.write('</body></html>');
        w.print();
        w.close();

        return true;
    }
</script>